import { NextResponse } from 'next/server'
import mysql from 'mysql2/promise'
import pool from '@/lib/db'
 
// 创建全局的 MySQL 连接池

 
export async function GET(request: Request) {
  console.log('API Route: /api/getData called')
  try {
    // 从连接池中获取连接
    const connection = await pool.getConnection()
    console.log('Connected to MySQL database')
 
    // 执行 MySQL 查询
    const [rows, fields] = await connection.query('SELECT * FROM user')
 
    // 释放连接回连接池
    connection.release()
 
    return NextResponse.json({ data: rows }, { status: 200 })
  } catch (error) {
    console.error('Error:', error)
    return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })
  }
}